package cn.com.chengmandian.core.db.utils;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.po.LikeTable;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

public class GeneratorUtil {
    private static String DEFAULT_URL = "jdbc:mysql://rm-bp141660286e94f6o7o.mysql.rds.aliyuncs.com:3306/test_bike?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai";
    private static String DEFAULT_USERNAME = "xh_test";
    private static String DEFAULT_PASSWORD = "xingheng@123";

    public static void gen(String dbName, String packageName, String tableName, String output) {
        gen(DEFAULT_URL.replace("test_bike", dbName), DEFAULT_USERNAME, DEFAULT_PASSWORD, packageName, tableName, null, output);
    }

    public static void gen(String packageName, String tableName, String output) {
        gen(DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD, packageName, tableName, null, output);
    }

    public static void genAll(String dbName, String packageName, String tablePre, String output) {
        gen(DEFAULT_URL.replace("test_bike", dbName), DEFAULT_USERNAME, DEFAULT_PASSWORD, packageName, null, tablePre, output);
    }

    public static void genAll(String packageName, String tablePre, String output) {
        gen(DEFAULT_URL, DEFAULT_USERNAME, DEFAULT_PASSWORD, packageName, null, tablePre, output);
    }

    /**
     * 自动生成代码
     * @param url 数据库url
     * @param username 数据库用户名
     * @param password 数据库密码
     * @param tableName 表名
     * @param output 生成目录
     */
    public static void gen(String url, String username, String password, String packageName, String tableName, String tablePre, String output) {
        FastAutoGenerator.create(url, username, password).globalConfig(builder -> {
            builder.author("AutoGenerator")
                    .fileOverride()
                    .enableSwagger()
                    .dateType(DateType.ONLY_DATE)
                    .outputDir(output);
        }).packageConfig(builder -> {
            builder.parent(packageName)
                    .entity("model")
                    .pathInfo(Collections.singletonMap(OutputFile.mapperXml, output));
        }).templateConfig(builder -> {
            builder.entity("ftl/entity.java");
        }).strategyConfig(builder -> {
            if (StrUtil.isNotBlank(tableName)) {
                builder.addInclude(tableName);
            } else if (StrUtil.isNotBlank(tablePre)) {
                builder.likeTable(new LikeTable(tablePre, SqlLike.RIGHT));
            }
            builder.entityBuilder().enableLombok()
                    .logicDeleteColumnName("is_deleted")
                    .controllerBuilder().enableRestStyle()
                    .mapperBuilder().enableMapperAnnotation();
        }).templateEngine(new FreemarkerTemplateEngine()).execute();
    }
}
